package com.controller;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import com.model.Employee;
import com.service.BranchService;
import com.service.EmployeeService;

@Controller
public class EmployeeController {
	@Autowired
	private EmployeeService employeeService;
	@Autowired
	private BranchService branchService;
	
	
	// REquest to add employee.. Give blank form of employee
	@RequestMapping(value="/user/addEmployee",method=RequestMethod.GET)
	public String addEmployee(@ModelAttribute("command") Employee employee) {
		return "employee";
	}
	
	// add employee details into database
	@RequestMapping(value = "/user/saveEmployee", method = RequestMethod.POST)
	public String saveEmployee(HttpSession session,@ModelAttribute("command") Employee employee) {
			employeeService.add(session.getAttribute("branch").toString(),employee);
			return "redirect:/user/addEmployee.htm";
	}
	
	// give employee details from database
	@RequestMapping("/user/editEmployee")
    public ModelAndView editEmployee(@ModelAttribute("command") Employee employee,
    		HttpServletRequest request)
    {
		Integer id=Integer.parseInt(request.getParameter("id"));
		Map<String, Object> model = new HashMap<String, Object>();
		model.put("employee",employeeService.get(id));
        return new ModelAndView("employee",model);
    }
	
	//delete Employee
	@RequestMapping("/user/deleteEmployee")
    public String deleteEmployee(HttpServletRequest request)
    {
		Integer id=Integer.parseInt(request.getParameter("id"));
        employeeService.delete(id);
        return "redirect:/user/addEmployee.htm";
    }

}
